package com.javafactroy.kunlong.framework.generalthread.base;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
public class TGeneralData<T> {

    /**
     * 线程数
     * */
    int threadNum = 4;
    /**
     * 并发执行超时时间
    * */
    int threadWaitMinutes = 5;

    /*
    * 原始数据
    * */
    List<T> list;
    /*
    * 并发拆分列表
    * */
    List<List<T>> groupList;


    // 拆分
    public TGeneralData<T> split2Group() {
        if (ObjectUtil.isEmpty(list)) {

            groupList = new ArrayList<>();
            return this;
        }

        if (threadNum <= 1) {
            groupList = new ArrayList<>();
            groupList.add(list);
            return this;
        }

        return split();
    }

    TGeneralData<T> split() {


        int size = list.size() / threadNum;
        if (list.size() % threadNum > 0) {
            size += 1;
        }
        groupList = Lists.partition(list, size);

        return this;

    }

}
